package com.market.cash.analyse.util

import android.app.Application
import android.os.Bundle
import com.facebook.FacebookSdk
import com.facebook.appevents.AppEventsConstants
import com.facebook.appevents.AppEventsLogger
import com.market.cash.analyse.AnalyseBuilder
import com.market.cash.analyse.bean.FacebookBean
import com.market.cash.analyse.type.IAnalyseEvent
import java.math.BigDecimal
import java.math.RoundingMode
import java.util.Currency

object FacebookUtil {

    private var _appEventsLogger: AppEventsLogger? = null

    /**
     * 初始化facebook模块
     *
     * @param context Application
     */
    fun init(context: Application) {
        try {
            if (_appEventsLogger == null) {
                FacebookSdk.sdkInitialize(context)
                AppEventsLogger.activateApp(context)
                _appEventsLogger = AppEventsLogger.newLogger(context)
            }

            if (AnalyseBuilder.zone.isBlank()) return
            saveS2SInfo()
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 保存facebook s2s信息
     */
    private fun saveS2SInfo() {
        val path = AnalyseBuilder.pathFacebookInstall
        val url = UrlHelper.combineUrl()
        val body = FacebookBean().encrypt(path)
        AnalysisHttpHelper.doPostAction(url, body)
    }

    /**
     * 打点
     *
     * @param event 事件名
     */
    fun addEvent(event: IAnalyseEvent) {
        _appEventsLogger?.let {
            try {
                when (event) {
                    IAnalyseEvent.AT_REGISTER -> {
                        val params = Bundle()
                        params.putString(AppEventsConstants.EVENT_PARAM_REGISTRATION_METHOD, event.name)
                        it.logEvent(AppEventsConstants.EVENT_NAME_COMPLETED_REGISTRATION, params)
                    }

                    IAnalyseEvent.AT_LOAN_APPLY -> {
                        val params = Bundle()
                        params.putInt(AppEventsConstants.EVENT_PARAM_NUM_ITEMS, 1)
                        params.putString(AppEventsConstants.EVENT_PARAM_CONTENT_TYPE, event.name)
                        params.putString(AppEventsConstants.EVENT_PARAM_CONTENT_ID, event.name)
                        params.putString(AppEventsConstants.EVENT_PARAM_CURRENCY, "USD")
                        it.logPurchase(BigDecimal.valueOf(1), Currency.getInstance("USD"), params)
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    /**
     * 获取总存储大小
     */
    fun getTotalRom(): String {
        var totalRom = ""
        if (totalRom.endsWith("TB")) {
            totalRom = totalRom.replace("TB".toRegex(), "")
            totalRom = BigDecimal(totalRom).multiply(BigDecimal(1024)).stripTrailingZeros().toPlainString()
        } else if (totalRom.endsWith("GB")) {
            totalRom = totalRom.replace("GB".toRegex(), "")
        } else if (totalRom.endsWith("MB")) {
            totalRom = totalRom.replace("MB".toRegex(), "")
            totalRom = BigDecimal(totalRom).divide(BigDecimal(1024), RoundingMode.HALF_UP).stripTrailingZeros().toPlainString()
        }
        return totalRom
    }

    /**
     * 获取可用存储大小
     */
    fun getAvalidableRom(): String {
        var totalRom = ""
        if (totalRom.endsWith("TB")) {
            totalRom = totalRom.replace("TB".toRegex(), "")
            totalRom = BigDecimal(totalRom).multiply(BigDecimal(1024)).stripTrailingZeros().toPlainString()
        } else if (totalRom.endsWith("GB")) {
            totalRom = totalRom.replace("GB".toRegex(), "")
        } else if (totalRom.endsWith("MB")) {
            totalRom = totalRom.replace("MB".toRegex(), "")
            totalRom = BigDecimal(totalRom).divide(BigDecimal(1024), RoundingMode.HALF_UP).stripTrailingZeros().toPlainString()
        }
        return totalRom
    }
}